﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathLab.Messaging;
using MathLab.Utils;
using MathLab.Exceptions;

namespace MathLab.Interpretation
{
    /// <summary>
    /// Class holding arays data.
    /// </summary>
    class DataHolder
    {
        private Dictionary<string, Matrix> arrays;

        public DataHolder()
        {
            arrays = new Dictionary<string, Matrix>();

            arrays.Add("A", new Matrix("A", new double[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }));
            arrays.Add("B", new Matrix("B", new double[,] { { 3, 4 }, { 3, 48 }, { 52, 64 }, { 17, 8 } }));
            arrays.Add("C", new Matrix("C", new double[,] { { 1 }, { 2 }, { 3 } }));
            arrays.Add("D", new Matrix("C", new double[,] { { 4 }, { 5 }, { 6 } }));
        }

        public void addArray(Matrix array)
        {
            if (arrays.ContainsKey(array.name))
            {
                arrays.Remove(array.name);
            }

            arrays.Add(array.name, array);          
        }

        public Matrix getArray(string name)
        {
            Matrix result = null;
            if (arrays.ContainsKey(name))
            {
                result = arrays[name];
            }
            else
            {
                throw new ArrayNotFoundException(name);
            }
            return result;
        }

        public string getArrayAsString(string name)
        {
            string result = null;

            if (arrays.ContainsKey(name))
            {
                result = StringUtils.arrayToString(arrays[name]);
            }
            else
            {
                throw new ArrayNotFoundException(name);
            }

            return result;
        }

        public string getArrayAsString(Matrix array)
        {
            return StringUtils.arrayToString(array);
        }

        public string getArrayAsString(double[,] array)
        {
            return StringUtils.arrayToString(array);
        }
    }
}
